package actions;

import java.util.ArrayList;
import java.util.Map;

import javax.ejb.EJB;

import org.apache.struts2.ServletActionContext;

import beans.BookManager;
import beans.BookManagerBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;

public class DeleteBooksAction extends ActionSupport {

	@EJB
	private BookManager bookManager;

	private Item item = new Item();

	public String execute() {

		Map session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("userId");
		if (id == null) {
			this.addActionError("User ID is null");
			return ERROR;
		}

		bookManager = new BookManagerBean();
		String copyid = ServletActionContext.getRequest().getParameter("bkId");
		int val = Integer.parseInt(copyid.trim());

		Item item = bookManager.getCopy(val);
		if (item.getCopyStatus() == Item.loaned) {
			this.addActionError("Cannot delete a borrowed item");
			return "ErrorInDelete";
		}

		if (bookManager.deleteCopy(item.getCopyId()))
			return SUCCESS;

		this.addActionError("Book could not be deleted");
		return "ErrorInDelete";

	}

}